package com.jhs.kjs.handler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Controller;

import com.jhs.kjs.dto.UserDTO;
import com.jhs.kjs.model.Authority;

@Controller
public class UserDetailsServiceImpl implements AuthenticationProvider {
	
	private UserDTO user = new UserDTO();
//	private static final Set<String> setAdmin;
//	static {
//		setAdmin = new HashSet<String>();
//		setAdmin.add("admin");
//	}

//	private boolean isAdmin(Object principal) {
//		return setAdmin.contains(principal);
//	}

	boolean isAuthenticatedUser(Object id, Object pw) {
		user.setUserId(id.toString());
		user.setUserPw(pw.toString());
		
		
		try {
			MySQLAccess dao = new MySQLAccess();
		    int auth = dao.readDataBase(id.toString(), pw.toString());
		    if(auth>0){
		    	user.setLevel(auth);
		    	return true;
		    }
		    return false;
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return user == null;
	}

	@Override
	public Authentication authenticate(Authentication authentication)
			throws AuthenticationException {

		Object principal = authentication.getPrincipal();
		Object password = authentication.getCredentials();

		// 여러분의 Authentication, Authorization 검증 수단!
		if (!isAuthenticatedUser(principal, password)) {
			throw new BadCredentialsException("Username does not match for "
					+ principal);
		}

		// 인증이 성공했다면...
		Collection<SimpleGrantedAuthority> roles = new ArrayList<SimpleGrantedAuthority>();
		
		switch(user.getLevel()){
		case 40 :{
			roles.add(new SimpleGrantedAuthority(Authority.ROLE_QADMIN.name()));	// 큐로 관리자
		}
		case 30 : {
			roles.add(new SimpleGrantedAuthority(Authority.ROLE_DADMIN.name()));	// 대리점 관리자
		}
		case 20 :{
			roles.add(new SimpleGrantedAuthority(Authority.ROLE_CADMIN.name()));	// 고객사 관리자
		}
		case 10 :{
			roles.add(new SimpleGrantedAuthority(Authority.ROLE_USER.name()));	//고객사 사용자
		}
		}
//		if (isAdmin(principal)) {
//			roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
//		}
		return new UsernamePasswordAuthenticationToken(principal, password,
				roles); // 토큰 발급 해당 토큰은 Spring-Security에서 SecurityContext로 할당
						// 된다.
	}

	@Override
	public boolean supports(Class<?> clazz) {
		return clazz.equals(UsernamePasswordAuthenticationToken.class);
	}

}
